import HeyUI from 'heyui';
export default ({ $axios }) => {
    // 数据访问前缀
    //$axios.defaults.baseURL = 'http://127.0.0.1:9191';
    $axios.defaults.withCredentials = true;      // 允许携带cookie
    $axios.defaults.crossDomain = true;
    $axios.defaults.dataType = 'json';

    $axios.defaults.timeout = 80000 // 设置请求超时
    $axios.defaults.headers.post['Content-Type'] = 'application/json' // 请求头的设置

    $axios.interceptors.request.use(
        config => {
            HeyUI.$LoadingBar.start();
            return config
        },
        err => {
            HeyUI.$LoadingBar.fail();
            return Promise.reject(err)
        });
    $axios.interceptors.response.use(
        response => {
            HeyUI.$LoadingBar.success();
            if (response.data.code == 401) {
                HeyUI.$Message.error("登录已过期");
            } else if (response.data.code == 404) {
                HeyUI.$Message.error("请求地址无效");
            }
            else {
                if (response.data.code != undefined && response.data.code != 200) {
                    HeyUI.$Message.error(response.data.msg);
                }
                return response
            }
        },
        error => {
            if (error.response.status == 401) {
                HeyUI.$Message.error("登录已过期");
            }
            return Promise.reject(error.response)
        })
}

